package com.flower8.controller;

import com.flower8.dto.ActiveAwardlogDto;
import com.flower8.dto.UserDto;
import com.flower8.service.DataStatistService;
import com.flower8.sysconfig.constants.MsgConstants;
import com.flower8.utils.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 数据统计控制层
 * @Author: tanglin
 * @Date: 2019/09/26 17:10
 * @Version: 1.0
 */
@RestController
@RequestMapping("/dataStatist")
public class DataStatistController extends BaseController{
    private static final Logger log = LoggerFactory.getLogger(DataStatistController.class);

    @Autowired
    private DataStatistService dataStatistService;

    /**
     * 统计抢红包分布情况
     * @return
     */
    @RequestMapping("/getRedPagData")
    public Object getRedPagData(){
        logBefore(log, "++++++++++ 统计抢红包分布情况 ++++++++++");
        try {
            List<Map> data = dataStatistService.getRedPagData();
            logAfter(log);
            return data;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return null;
        }
    }

    /**
     * 统计大转盘抽奖分布情况
     * @return
     */
    @RequestMapping("/getBigWheelData")
    public Object getBigWheelData(){
        logBefore(log, "++++++++++统计大转盘抽奖分布情况 ++++++++++");
        try {
            List<Map> data = dataStatistService.getBigWheelData();
            logAfter(log);
            return data;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return null;
        }
    }

    /**
     * 统计总的抽奖数量
     * @return
     */
    @RequestMapping("/getDataTotal")
    public Object getDataTotal(){
        logBefore(log, "++++++++++统计总的抽奖数量 ++++++++++");
        try {
            Map data = dataStatistService.getDataTotal();
            logAfter(log);
            return responseResultForSuccess(data);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 查询红包明细列表
     * @param request
     * @return
     */
    @RequestMapping("/getRedPagDetailList")
    public Object getRedPagDetailList(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询红包明细列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<ActiveAwardlogDto> data =  dataStatistService.getRedPagDetail(pd);
            int total = dataStatistService.getRedPagDetailCount(pd);
            res.put("data", data);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 查询红包明细总次数和总金额
     * @param request
     * @return
     */
    @RequestMapping("/getRedPagDetailTotal")
    public Object getRedPagDetailTotal(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询红包明细总次数和总金额 ++++++++++");
        PageData pd = new PageData(request);
        try {
            Map data = dataStatistService.getRedPagDetailTotal(pd);
            logAfter(log);
            return responseResultForSuccess(data);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 查询大转盘明细列表
     * @param request
     * @return
     */
    @RequestMapping("/getBigWheelDetailList")
    public Object getBigWheelDetailList(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询红包明细列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<ActiveAwardlogDto> data =  dataStatistService.getBigWheelDetail(pd);
            int total = dataStatistService.getBigWheelDetailCount(pd);
            res.put("data", data);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 查询大转盘明细总次数和总金额
     * @param request
     * @return
     */
    @RequestMapping("/getBigWheelDetailTotal")
    public Object getBigWheelDetailTotal(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询大转盘明细总次数和总金额 ++++++++++");
        PageData pd = new PageData(request);
        try {
            Map data = dataStatistService.getBigWheelDetailTotal(pd);
            logAfter(log);
            return responseResultForSuccess(data);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 获取注册人数统计
     * @param request
     * @return
     */
    @RequestMapping("/registerData")
    public Object registerData(HttpServletRequest request){
        logBefore(log, "++++++++++ 获取注册人数统计 ++++++++++");
        PageData pd = new PageData(request);
        try {
            List<Map> data = dataStatistService.registerData(pd);
            logAfter(log);
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("dataList", data);
            return responseResultForSuccess(dataMap);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }


    /**
     * 查询发红包列表
     * @param request
     * @return
     */
    @RequestMapping("/getRedbagList")
    public Object getRedbagList(HttpServletRequest request){
        logBefore(log, "++++++++++ 查询发红包列表 ++++++++++");
        PageData pd = new PageData(request);

        pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
        pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

        Map res = new HashMap();
        try {
            List<Map> data =  dataStatistService.getRedbagList(pd);
            int total = dataStatistService.getRedbagListCount(pd);
            res.put("data", data);
            res.put("recordsTotal", total);   //dataTable分页需要
            res.put("recordsFiltered", total);//dataTable分页需要
            res.put("draw", pd.get("draw").toString());//dataTable分页需要
            logAfter(log);
            Map<String, Object> dataMap = (Map<String, Object>) responseResultForSuccess(res);
            // 兼容旧网页
            dataMap.put("recordsTotal", total);   //dataTable分页需要
            dataMap.put("recordsFiltered", total);//dataTable分页需要
            dataMap.put("draw", pd.get("draw").toString());//dataTable分页需要
            return dataMap;
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

    /**
     * 获取发红包总金额
     * @param request
     * @return
     */
    @RequestMapping("/getRedbagTotal")
    public Object getRedbagTotal(HttpServletRequest request){
        logBefore(log, "++++++++++ 获取发红包总金额 ++++++++++");
        PageData pd = new PageData(request);
        try {
            Map data = dataStatistService.getRedbagTotal(pd);
            logAfter(log);
            return responseResultForSuccess(data);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return responseResultForError(MsgConstants.ERROR_99001_MSG);
        }
    }

}
